package com.sunday.aomicaichang.ui.product;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sunday.common.adapter.MainFragmentPagerAdapter;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.event.EventBus;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.event.UpdateProductList;
import com.sunday.member.constv.ConstValue;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchProductListActivity extends BaseActivity {


    @Bind(R.id.btn_back)
    ImageView btnBack;
    @Bind(R.id.edit_query)
    EditText editQuery;
    @Bind(R.id.tab_layout)
    TabLayout tabLayout;
    @Bind(R.id.view_pager)
    ViewPager viewPager;
    final static int NO_ICON = 0x123;
    private List<String> titles = new ArrayList<>();
    private int[] tabIcons = {
            R.mipmap.triangle_down,
            NO_ICON,
            NO_ICON
    };

    private int catId=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_product_list);
        ButterKnife.bind(this);
        catId=getIntent().getIntExtra("catId",0);
        initView();
        initQuery();
    }


    private void initQuery(){
        editQuery.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        editQuery.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH
                        || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    String name = editQuery.getText().toString().trim();
                    /*隐藏软键盘*/
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (inputMethodManager.isActive()) {
                        inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
                    }
                    for (int i=0;i<fragments.size();i++){
                        ((ProductListFragment)fragments.get(i)).setQueryName(name);
                    }
                    EventBus.getDefault().post(new UpdateProductList());
                    return true;
                } else {
                    return false;
                }
            }

        });
    }

    private void initView(){
        titles.add("综合排序");
        titles.add("销量");
        titles.add("价格");
        initFragment();
        tabLayout.getTabAt(0).setCustomView(getFirstTabView());

    }

    public View getFirstTabView(){
        View view= LayoutInflater.from(mContext).inflate(R.layout.layout_tabview,null);
        TextView txt_title = (TextView) view.findViewById(R.id.tab_title);
        txt_title.setText(titles.get(0));
        ImageView image_title = (ImageView) view.findViewById(R.id.tab_icon);
        image_title.setImageResource(R.mipmap.triangle_down);
        return view;
    }

    private ArrayList<Fragment> fragments=new ArrayList<>();
    private void initFragment(){
        fragments.add(ProductListFragment.newInstance("",catId,null,null));
        fragments.add(ProductListFragment.newInstance("",catId, ConstValue.PRO_SALENUM,ConstValue.ASC));
        fragments.add(ProductListFragment.newInstance("",catId,ConstValue.PRO_PRICE,ConstValue.ASC));
        viewPager.setAdapter(new MainFragmentPagerAdapter(getSupportFragmentManager(),fragments,titles));
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setCurrentItem(0,false);
        viewPager.setOffscreenPageLimit(3);
        /*tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });*/
    }

    @OnClick(R.id.btn_back)
    void back(){
        onBackPressed();
    }


}
